import CryptoJS from './cryptojs.js'
/** 获取签名 start */

function toUint8Array(wordArray) {
	// Shortcuts
	const words = wordArray.words;
	const sigBytes = wordArray.sigBytes;

	// Convert
	const u8 = new Uint8Array(sigBytes);
	for (let i = 0; i < sigBytes; i++) {
		u8[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
	}
	return u8;
}

function Uint8ArrayToString(fileData) {
	let dataString = '';
	for (let i = 0; i < fileData.length; i++) {
		dataString += String.fromCharCode(fileData[i]);
	}
	return dataString;
}
// 签名函数示例
function signCallback(signStr, secretKey) {
	const hash = CryptoJS.HmacSHA1(signStr, secretKey);
	const bytes = Uint8ArrayToString(toUint8Array(hash));

	// return uni.arrayBufferToBase64(toUint8Array(hash));
  return arrayBufferToBase64(toUint8Array(hash))
}

function arrayBufferToBase64(buffer) {
  var binary = '';
  var bytes = new Uint8Array(buffer);
  var len = bytes.byteLength;
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i]);
  }
  return window.btoa(binary);
}
/** 获取签名 end */


export default {
	signCallback,
	toUint8Array,
	Uint8ArrayToString
}
